package cn.accp.bookShop.entity;

import java.util.Scanner;

import cn.accp.bookShop.base.bookDao;
import cn.accp.bookShop.base.impl.ArrayBookDao;

public class Shop {

	public Shop() {
		this.dao = new ArrayBookDao();
		this.input = new Scanner(System.in);
	}

	// 输入
	private Scanner input;
	// 书的方法接口
	private bookDao dao;

	public int showMenu() {
		int choice;
		do {
			System.out.println("请选择菜单：");
			System.out.println("1.显示图书信息");
			System.out.println("2.添加图书");
			System.out.println("3.修改单价");
			System.out.println("4.删除图书");
			System.out.println("5.退出");
			choice = input.nextInt();
		} while (choice < 1 || choice > 5);
		return choice;
	}

	public void addBook() {
		Book book = new Book();
		int temp = -1, flag = -1;
		do {
			if (flag >= 0) {
				System.out.println("编号重复，请重新输入编号:");
			} else {
				System.out.println("请输入书的编号:");
			}
			temp = input.nextInt();
			flag = ((ArrayBookDao) this.dao).indexOf(temp);
		} while (flag >= 0);
		book.setId(temp);
		System.out.println("请输入书名:");
		book.setTitle(input.next());
		System.out.println("请输入单价:");
		book.setPrice(input.nextDouble());
		System.out.println("请输入出版社:");
		book.setPublisher(input.next());
		dao.add(book);
	}

	public void removeBook() {
		int temp;
		System.out.println("请输入书的编号:");
		temp = input.nextInt();
		if (dao.remove(temp)) {
			System.out.println("编号为" + temp + "的书已删除!");
		} else {
			System.out.println("编号为" + temp + "的书不存在!");
		}
	}

	public void updateBook() {
		int temp;
		double newPrice;
		System.out.println("请输入书的编号:");
		temp = input.nextInt();
		System.out.println("请输入该书的新价格:");
		newPrice = input.nextDouble();
		if (dao.update(temp, newPrice)) {
			System.out.println("编号为" + temp + "的书的单价已更新为" + newPrice + ".");
		} else {
			System.out.println("编号为" + temp + "的书不存在!");
		}
	}

	public void showBooks() {
		Book[] items = ((ArrayBookDao) dao).getItems();
		int top = ((ArrayBookDao) dao).getTop();
		for (int i = 0; i < top; i++) {
			items[i].showMe();
		}
	}
}
